package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.bo.GoodsCombineBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsSpecification;
import com.cskaoyan.market.service.admin.MarketGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @MethodName
 * @Description 商品管理部分接口
 * @Author M
 * @Date 15:16 2023/5/9
 **/
@RequiresAuthentication
@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {

    @Autowired
    private MarketGoodsService goodsService;

    /**
     * @MethodName catAndBrand
     * @Description 品牌商信息
     * @param: req
     * @param: resp
     * @Author M
     * @Date 17:29 2023/5/9
     **/
    @GetMapping("catAndBrand")
    public Object catAndBrand() {
        Map<String, Object> result = goodsService.catAndBrand();
        return ResponseUtil.ok(result);
    }

    // 实现商品列表功能
    // 请求参数：
    // page=1&limit=20&goodsSn=2&name=3&sort=add_time&order=desc&goodsId=1
    @GetMapping("list")
    public Object list(String page, String limit, String goodsSn, String name,String goodsId, String sort, String order) {
        // 获取请求参数
        // 对参数值为null的添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 对page与limit进行校验，不能为非数字
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            // 发生错误，返回
            return ResponseUtil.badArgument();
        }
        // 校验成功，进行查询操作
        List<MarketGoods> marketGoodsList = goodsService.list(pageInt, limitInt, goodsSn, sort, order, goodsId, name);
        return ResponseUtil.okList(marketGoodsList);
    }

    /**
     * @MethodName create
     * @Description 上架商品
     * @param: req
     * @param: resp
     * @Author M
     * @Date 15:58 2023/5/9
     **/
    @PostMapping("create")
    public Object create(@RequestBody GoodsCombineBo combineBo) throws IOException {
        // 校验
        boolean result = valid(combineBo);
        if (!result) {
            //校验没有通过
            return ResponseUtil.badArgument();
        }
        int row = goodsService.addGoods(combineBo.getGoods(), combineBo.getSpecifications(), combineBo.getAttributes(), combineBo.getProducts());
        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    // 判断是否为空
    public boolean valid(GoodsCombineBo combineBo) {
        MarketGoods goods = combineBo.getGoods();
        List<MarketGoodsSpecification> specifications = combineBo.getSpecifications();
        // 判断商品名称是否为空
        if (StringUtils.isEmpty(goods.getName())) {
            return false;
        }
        for (MarketGoodsSpecification specification : specifications) {
            // 判断规格是否为空
            if (StringUtils.isEmpty(specification.getSpecification())) {
                return false;
            }
            if (StringUtils.isEmpty(specification.getValue())) {
                return false;
            }
        }
        return true;
    }

}
